package com.sz.admin.role.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.role.service.RoleService;
import com.sz.admin.role.pojo.po.Role;
import com.sz.admin.role.mapper.RoleMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.role.pojo.dto.RoleCreateDTO;
import com.sz.admin.role.pojo.dto.RoleUpdateDTO;
import com.sz.admin.role.pojo.dto.RoleListDTO;
import com.sz.admin.role.pojo.dto.RoleImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.role.pojo.vo.RoleVO;

/**
 * <p>
 * 系统角色定义 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-06-30
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public void create(RoleCreateDTO dto){
        Role role = BeanCopyUtils.copy(dto, Role.class);
        save(role);
    }

    @Override
    public void update(RoleUpdateDTO dto){
        Role role = BeanCopyUtils.copy(dto, Role.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(Role::getRoleId, dto.getRoleId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(role);
    }

    @Override
    public PageResult<RoleVO> page(RoleListDTO dto){
        Page<RoleVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), RoleVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<RoleVO> list(RoleListDTO dto){
        return listAs(buildQueryWrapper(dto), RoleVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public RoleVO detail(Object id){
        Role role = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(role);
        return BeanCopyUtils.copy(role, RoleVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<RoleImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), RoleImportDTO.class, true);
        List<RoleImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(RoleListDTO dto, HttpServletResponse response) {
        List<RoleVO> list = list(dto);
        String fileName = "系统角色定义模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "系统角色定义", RoleVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(RoleListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(Role.class);
        if (Utils.isNotNull(dto.getRoleName())) {
            wrapper.like(Role::getRoleName, dto.getRoleName());
        }
        if (Utils.isNotNull(dto.getCreatedAt())) {
            wrapper.eq(Role::getCreatedAt, dto.getCreatedAt());
        }
        if (Utils.isNotNull(dto.getUpdatedAt())) {
            wrapper.eq(Role::getUpdatedAt, dto.getUpdatedAt());
        }
        if (Utils.isNotNull(dto.getIsDeleted())) {
            wrapper.eq(Role::getIsDeleted, dto.getIsDeleted());
        }
        return wrapper;
    }
}